package com.ruoyi.web.service.Impl;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.pinyin.PinYinUtils;
import com.ruoyi.web.domain.MesAllotDetail;
import com.ruoyi.web.domain.MesAllotInfo;
import com.ruoyi.web.domain.mes.MesPaper;
import com.ruoyi.web.domain.vo.AllotDetailVo;
import com.ruoyi.web.mapper.MesConfigMapper;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.stream.Collectors;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.web.mapper.MesAllotInfoMapper;
import com.ruoyi.web.service.IMesAllotInfoService;

import javax.annotation.Resource;

/**
 * 调拨单基本信息Service业务层处理
 * 
 * @author wxt
 * @date 2024-01-18
 */
@Service
public class MesAllotInfoServiceImpl implements IMesAllotInfoService 
{
    @Resource
    private MesAllotInfoMapper mesAllotInfoMapper;
    @Resource
    private MesPaperService mesPaperService;

    @Resource
    private MesConfigMapper configMapper;

    /**
     * 查询调拨单基本信息
     * 
     * @param id 调拨单基本信息主键
     * @return 调拨单基本信息
     */
    @Override
    public MesAllotInfo selectMesAllotInfoById(Long id)
    {
        return mesAllotInfoMapper.selectMesAllotInfoById(id);
    }

    /**
     * 查询调拨单基本信息列表
     * 
     * @param mesAllotInfo 调拨单基本信息
     * @return 调拨单基本信息
     */
    @Override
    public List<MesAllotInfo> selectMesAllotInfoList(MesAllotInfo mesAllotInfo)
    {
        return mesAllotInfoMapper.selectMesAllotInfoList(mesAllotInfo);
    }
    @Override
    public List<AllotDetailVo> selectMesDetaillist(Map<String,Object> params)
    {
        List<AllotDetailVo> allotDetailVos = mesAllotInfoMapper.selectMesDetaillist(params);
        BigDecimal decimal = mesAllotInfoMapper.selectMesDetaillistSumTotal(params);
        if(allotDetailVos.size() > 0){
            allotDetailVos.get(0).setTotalSum(decimal);
        }
        return allotDetailVos;
    }

    @Override
    public List<AllotDetailVo> exportMesDetaillist(Map<String,Object> params)
    {
        List<AllotDetailVo> allotDetailVos = mesAllotInfoMapper.exportMesDetaillist(params);
        BigDecimal decimal = mesAllotInfoMapper.selectMesDetaillistSumTotal(params);
        if(allotDetailVos.size() > 0){
            allotDetailVos.get(0).setTotalSum(decimal);
        }
        return allotDetailVos;
    }


    /**
     * 新增调拨单基本信息
     * 
     * @param mesAllotInfo 调拨单基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMesAllotInfo(MesAllotInfo mesAllotInfo)
    {
        mesAllotInfo.setCreateTime(DateUtils.getNowDate());

        // 生成调拨单编号
        //int maxNum = mesAllotInfoMapper.getAllotInfoMaxId();

        Map<String, Object> map = new HashMap<>();
        map.put("columnName", "allot_num");
        map.put("tableName", "mes_allot_info");
        int maxNum = configMapper.getMaxId(map);

        String code = PinYinUtils.getCodeNormal(maxNum, "ZZDBD");
        mesAllotInfo.setAllotNum(code);

        int rows = mesAllotInfoMapper.insertMesAllotInfo(mesAllotInfo);
        insertMesAllotDetail(mesAllotInfo);
        return rows;
    }

    /**
     * 修改调拨单基本信息
     * 
     * @param mesAllotInfo 调拨单基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public String updateMesAllotInfo(MesAllotInfo mesAllotInfo)
    {
        int status=mesAllotInfo.getStatus();
        if (status==2){//提交
            MesAllotInfo Info= mesAllotInfoMapper.selectMesAllotInfoById(mesAllotInfo.getId());//查询数据库
            List<MesAllotDetail> mesAllotDetailList = Info.getMesAllotDetailList();

                if (mesAllotDetailList.size()!=0) {
                // 获取纸张ids
                List<Long> listId = mesAllotDetailList.stream().map(it -> it.getPaperid()).collect(Collectors.toList());
                Long[] ids = listId.toArray(new Long[0]);
                // 根据纸张ids查询纸张库存明细
                List<MesPaper> detailList = mesPaperService.selectMesPaperByIds(ids);
                for (MesAllotDetail mesAllotDetail : mesAllotDetailList){
                    for (MesPaper paper:detailList){
                        if (paper.getId().equals(mesAllotDetail.getPaperid())) {
                            BigDecimal total = mesAllotDetail.getTotal();
                            int result = paper.getQuantity().compareTo(total);
                            if (result < 0) {
//                                return paper.getName() + "数量大于库存数量";
                                throw new ServiceException(paper.getName() + "数量大于库存数量");
                            }
                        }
                    }
                }
            }

//            for (MesAllotDetail mesAllotDetail : mesAllotDetailList){
//               BigDecimal total= mesAllotDetail.getTotal();
//                Long id= mesAllotDetail.getPaperid();
//                MesPaper paper=  mesPaperService.selectMesPaperById(id);
//                if ((paper.getQuantity()).compareTo(total)<0){
//                    return paper.getName()+ "数量大于库存数量";
//                }
//            }

            for (MesAllotDetail mesAllotDetail : mesAllotDetailList)
            {
                updateQuantity(mesAllotDetail.getPaperid(),mesAllotDetail.getTotal(),2);
            }
             mesAllotInfoMapper.updateMesAllotInfo(mesAllotInfo);
            return "提交成功";


        }else{
            mesAllotInfoMapper.deleteMesAllotDetailByAllotinfoid(mesAllotInfo.getId());//删除子表
            insertMesAllotDetail(mesAllotInfo);
            mesAllotInfoMapper.updateMesAllotInfo(mesAllotInfo);
            return "修改成功";
        }

//        MesAllotInfo Info= mesAllotInfoMapper.selectMesAllotInfoById(mesAllotInfo.getId());//查询数据库
//        mesAllotInfoMapper.deleteMesAllotDetailByAllotinfoid(Info.getId());//删除子表
//        //删除加库存
//        List<MesAllotDetail> mesAllotDetailList = Info.getMesAllotDetailList();
//        for (MesAllotDetail mesAllotDetail : mesAllotDetailList)
//        {
//            updateQuantity(mesAllotDetail.getPaperid(),mesAllotDetail.getTotal(),1);
//        }


    }

    /**
     * 批量删除调拨单基本信息
     * 
     * @param ids 需要删除的调拨单基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesAllotInfoByIds(Long[] ids)
    {
        mesAllotInfoMapper.deleteMesAllotDetailByAllotinfoids(ids);

//        for (long id : ids){
//            MesAllotInfo Info= mesAllotInfoMapper.selectMesAllotInfoById(id);
//            mesAllotInfoMapper.deleteMesAllotDetailByAllotinfoid(id);//删除调拨详情
//            List<MesAllotDetail> MesAllotDetailList = Info.getMesAllotDetailList();
//            for (MesAllotDetail mesDetail : MesAllotDetailList)
//            {
//                updateQuantity(mesDetail.getPaperid(),mesDetail.getTotal(),1);
//            }
//        }

        return mesAllotInfoMapper.deleteMesAllotInfoByIds(ids);
    }

    /**
     * 删除调拨单基本信息信息
     * 
     * @param id 调拨单基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesAllotInfoById(Long id)
    {
        mesAllotInfoMapper.deleteMesAllotDetailByAllotinfoid(id);

//        MesAllotInfo Info= mesAllotInfoMapper.selectMesAllotInfoById(id);
//        mesAllotInfoMapper.deleteMesAllotDetailByAllotinfoid(id);
//        List<MesAllotDetail> MesAllotDetailList = Info.getMesAllotDetailList();//删除加库存
//
//        for (MesAllotDetail mesAllotDetail : MesAllotDetailList)
//        {
//            updateQuantity(mesAllotDetail.getPaperid(),mesAllotDetail.getTotal(),1);
//        }

        return mesAllotInfoMapper.deleteMesAllotInfoById(id);
    }

    /**
     * 新增调拨单明细信息
     * 
     * @param mesAllotInfo 调拨单基本信息对象
     */
    public void insertMesAllotDetail(MesAllotInfo mesAllotInfo)
    {
        List<MesAllotDetail> mesAllotDetailList = mesAllotInfo.getMesAllotDetailList();
        Long id = mesAllotInfo.getId();
        if (StringUtils.isNotNull(mesAllotDetailList))
        {
            List<MesAllotDetail> list = new ArrayList<MesAllotDetail>();
            for (MesAllotDetail mesAllotDetail : mesAllotDetailList)
            {
                mesAllotDetail.setAllotinfoid(id);
                list.add(mesAllotDetail);
            }
            if (list.size() > 0)
            {
                mesAllotInfoMapper.batchMesAllotDetail(list);
            }
        }
    }

    private void updateQuantity(Long id, BigDecimal number, int type){
        MesPaper paper=new MesPaper();
        paper.setId(id);
        paper.setQuantity(number);
        paper.setQuantitymark(type);
        mesPaperService.updateMesPaperQuantity(paper);
    }

}
